<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/1
 * Time: 15:46
 */

namespace App\Weixin\Models;

use App\Base\Models\BaseModel;
use App\FreeBird\Models\InfoCommentModel;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 微信用户
 * Class WeixinUserModel
 * @package App\Weixin\Models
 */
class  WeixinUserModel extends BaseModel
{

    const CACHE_KEY = "wy:userinfo:";
    // use SoftDeletes;
    /**
     * 表名
     * @var string
     */
    public $table = "weixin_user";
    /**
     * 时间自动
     * @var bool
     */
    public $timestamps = true;
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'create_time';  //修改添加时间字段
    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'update_time';
    protected $appends = ["is_vip"];
    /**
     * 删除
     */
    // const DELETED_AT = 'delete_time';
    /**
     * 模型的日期字段保存格式。
     */
    protected $dateFormat = 'U';

    /**
     * @return WeixinUserIsvipModel
     */
    public function getWeixinUserIsvipModel(){
        return app()->make(WeixinUserIsvipModel::class);
    }

    public function getIsVipAttribute()
    {

        $lid = $this->lid;
        $uid = $this->uid;
        $time = time();
        $info = $this->getWeixinUserIsvipModel()->newQuery()->where("uid", $uid)->where("lid", $lid)->where("start_time", "<=", $time)->where("end_time", ">=", $time)->first();

        return empty($info) ?  0: 1;
    }

    public function comment()
    {
        return $this->hasMany(InfoCommentModel::class, "buid", "uid");
    }

}